#!/usr/bin/python
# -*- coding: utf-8 -*-

#from datetime import datetime
#from django.db import models
#from django.contrib.auth.models import User
#from net4.web.tags.models import Tag
from net4.middleware import threadlocals
#from net4.models.fields import RichTextField
from google.appengine.ext import db

class Post(db.Model):
    """"""
    title = db.StringProperty('Post标题')#models.CharField(u'Post标题', max_length=100, db_index=True)
    create_date = db.DateTimeProperty('发布时间', auto_now_add=True) #models.DateTimeField(u'发布时间', default=datetime.now(), editable=False)
    modified_date = db.DateTimeProperty('最后修改时间', auto_now=True) #models.DateTimeField(u'最后修改时间', default=datetime.now(), editable=False)
    author = db.UserProperty('作者') # models.ForeignKey(User, verbose_name=u"作者", editable=False)
    content = db.TextProperty('内容') # RichTextField(u'内容')
    last_visitor_IP = db.StringProperty('最后的访问者IP') #models.IPAddressField(u'最后的访问者IP', null=True, blank=True, editable=False)
    tags = db.StringListProperty('Tags') #models.ManyToManyField(Tag, verbose_name=u"the list of tags", null=True, blank=True)
    slug = db.URLProperty('自定义url') #models.SlugField(u'自定义url', max_length=100, null=True, blank=True)
    hits = db.IntegerProperty('点击数') #models.IntegerField(u'点击数', editable=False, default=0)
    comment_count = db.IntegerProperty('评论数', default=0) #models.IntegerField(u'评论数', editable=False, default=0)
    STATUS_CHOICES = (('0', u'草稿'),
                     ('1', u'正常发布'),
                     ('2', u'需要登录查看'),
                     ('3', u'私有'),
                     ('-1', u'删除'),
                     )
    #status = models.IntegerField(u'状态', choices=STATUS_CHOICES, radio_admin=True, default=1)
    status = db.CategoryProperty('状态', 
                                 choices=[c[0] for c in STATUS_CHOICES], 
                                 default='1')    
#    @models.permalink
#    def get_absolute_url(self):
#        return (u'posts.views.post_detail', (), {
#                    u'year': self.create_date.year,
#                    u'month': self.create_date.month,
#                    u'post_id': self.create_date.id,})
#    
##    def edit_url(self):
##        return (u'django.contrib.admin.views.main.change_stage', 
##                (u'posts', u'post', self.id))
##    edit_url = models.permalink(edit_url)
#    
    def save(self):
        """重载save，自动保存当前用户为作者"""
        if self.id is None:
            self.author = threadlocals.get_current_user()
        super(Post, self).save()
#    
#    @property
#    def can_edit(self):
#        return threadlocals.get_current_user().id == self.author_id
##    can_edit = property(__can_edit)
#                
#    class Meta:
#        get_latest_by = u'create_date'
#        ordering = (u'-create_date', u'title',)
##        permissions = (("poster", "Can manager post",),)
#    
#    class Admin:
#        list_display = (u'title', u'create_date', u'modified_date', u'author', u'hits',)
#        list_filter = (u'author', u'tags', u'create_date',)
#        search_fields = (u'title', u'slug',)
#        ordering = (u'-id',)
#        # various admin options are here
#        js = (u'/site_media/tiny_mce/tiny_mce.js',
#              u'/site_media/js/textareas.js',
#            )
    
    
#class Comment(models.Model):
#    title = models.CharField('主题', max_length=100, db_index=True)
#    username = models.CharField('大侠贵姓', max_length=50)
#    useremail = models.EmailField('Email(只有管理员才能看到)', blank=True)
#    usersite = models.URLField('你来自那里(你的blog或你的主页)', blank=True, verify_exists=False)
#    userIP = models.IPAddressField('用户IP', blank=True, editable=False)
#    content = RichTextField('想说就说')
#    post = models.ForeignKey(Post, verbose_name='相关Post', editable=False)
#    create_date = models.DateTimeField('发布时间', default=datetime.now(), editable=False)
#    modified_date = models.DateTimeField('最后修改时间', default=datetime.now(), editable=False)
#    
#    class Meta:
#        ordering = ['id']